﻿Imports System.Globalization
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Modules.Definitions
Imports DotNetNuke.Entities.Portals
Imports Aricie.DNN.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Lucene.Net.Documents
Imports Lucene.Net.Highlight

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers

    'todo: current algorithm is smelly (no mid nor tabid will be provided in params)
    Public Class LuceneSearchUrlRewriter
        Implements IUrlRewriterProvider
        Implements IIsActive


        Private Shared _WrappedModuleRewriters As New Dictionary(Of String, UrlProviderInfo)

        Public Function GetModuleRewrites() As List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites

            Dim toReturn As New List(Of GroupParamsRewrite)

            Dim desc As String = "search criteria component of the lucene search query, unescaped for Lucene Syntax"
            toReturn.Add(New GroupParamsRewrite("Search", desc, RewriteType.SubPath, UrlParam.FromSyntax("search")))
            desc = "Filter component of the lucene search query, unescaped for Lucene Syntax"
            toReturn.Add(New GroupParamsRewrite("Filter", desc, RewriteType.SubPath, UrlParam.FromSyntax("filter")))
            For Each fieldDef As FieldDefinition In PortalQueryingDriver.GetCurrent.GetSearchableFields() 'modifié pour ne plus montrer les champs de type SORT_
                Dim toAdd As New GroupParamsRewrite("Lucene" & fieldDef.Id, desc, RewriteType.SubPath, UrlParam.FromSyntax("filter"))
                toAdd.UrlParams(UrlParam.FromSyntax("filter")) = ParamNature.NonExclusive Or ParamNature.Mandatory
                toReturn.Add(toAdd)
            Next
            toReturn.AddRange(Me.GetAdditionalRewrites)
            Return toReturn

        End Function

        Public Overridable Function GetAdditionalRewrites() As IList(Of GroupParamsRewrite)
            Return New List(Of GroupParamsRewrite)
        End Function


        Public Overridable Function GetSpecificRewrite(ByRef result As GroupRewriteResult, ByVal groupName As String, ByVal objRewriteType As RewriteType, _
                                       ByVal params As Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As Boolean
            Return False
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, _
                                       ByVal params As Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult _
            Implements IUrlRewriterProvider.RewriteParams

            Dim toReturn As New GroupRewriteResult()

            Select Case groupName.ToLowerInvariant
                Case "search"
                    Dim param As String = params(UrlParam.FromSyntax("search"))
                    Dim unescapedParam As String = BusinessController.LuceneUnEscape(BusinessController.LuceneUnEscape(param))
                    'toReturn.RewriteValue = behaviour.StringEscaper.EscapeString(unescapedParam)
                    toReturn.RewriteValue = unescapedParam
                    toReturn.ConsumedParameters(UrlParam.FromSyntax("search")) = True
                Case Else
                    Dim param As String = params(UrlParam.FromSyntax("filter"))
                    Dim unescapedParam As String = BusinessController.LuceneUnEscape(BusinessController.LuceneUnEscape(param))
                    If groupName.ToLowerInvariant = "filter" Then
                        'toReturn.RewriteValue = behaviour.StringEscaper.EscapeString(unescapedParam)
                        toReturn.RewriteValue = unescapedParam
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("filter")) = True
                    ElseIf groupName.ToLowerInvariant.StartsWith("lucene") Then
                        Dim fieldName As String = groupName.Substring(6)

                        If Not String.IsNullOrEmpty(unescapedParam) Then
                            Dim terms As Dictionary(Of String, WeightedTerm) = BusinessController.GetTerms(NukeHelper.PortalId, unescapedParam)
                            If terms.ContainsKey(fieldName.ToLowerInvariant) Then
                                Dim value As String = GetTermValue(fieldName.ToLowerInvariant, terms)
                                If value <> "" Then
                                    toReturn.AddRewriteValue(value)
                                    Dim rangeField As String = fieldName & "2"
                                    If terms.ContainsKey(rangeField) Then
                                        toReturn.AddRewriteValue(GetTermValue(rangeField, terms))
                                    End If
                                End If
                                toReturn.ConsumedParameters(UrlParam.FromSyntax("filter")) = True
                            End If
                        End If
                        'strquery = HttpUtility.UrlDecode(strquery)
                    End If

            End Select

            Return toReturn
        End Function

        Protected Overridable Function GetTermValue(ByVal fieldName As String, ByVal terms As Dictionary(Of String, WeightedTerm)) As String
            Dim value As String = terms(fieldName).GetTerm
            Select Case fieldName.ToLowerInvariant
                Case DnnSearchItemField.PubDate.ToLowerInvariant _
                        , DnnSearchItemField.PubDate.ToLowerInvariant & "2" _
                        , LuceneGeneralField.IndexedDate.ToLowerInvariant _
                        , LuceneGeneralField.IndexedDate.ToLowerInvariant & "2"
                    value = DateField.StringToDate(value).ToString("ddMMyy")
            End Select
            Return value
        End Function


        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return DotNetNuke.Framework.Reflection.CreateType("Aricie.DNN.Modules.LuceneSearch.Business.BusinessController", True) IsNot Nothing
            End Get
        End Property
    End Class
End Namespace


